package com.njit.WeStudy.control;
import com.njit.WeStudy.dao.ClassMapper;
import com.njit.WeStudy.dao.InviteCodeMapper;
import com.njit.WeStudy.dao.JoinClassMapper;
import com.njit.WeStudy.dataobject.Class;
import com.njit.WeStudy.dataobject.InviteCode;
import com.njit.WeStudy.dataobject.JoinClass;
import com.njit.WeStudy.dataobject.User;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.util.List;
import java.util.UUID;

/**
 * @author yuruiqiao
 * @date 2020/2/5
 * @description 课堂控制类
 */
@RestController
public class ClassesController {

    @Resource
    private ClassMapper classMapper;

    @Resource
    private InviteCodeMapper inviteCodeMapper;

    @Resource
    private JoinClassMapper joinClassMapper;

    /**
     * @author yuruiqiao
     * @date 2020/2/12
     * @description 点击创建班级的按钮
     */
    @RequestMapping(path = "/createClass")
    public String createClass(HttpSession session, @RequestParam("className") String className,
                              @RequestParam("description")String description){
        try{
            int masterId = ((User) session.getAttribute("user")).getUserId();
            Class newClass = new Class();
            newClass.setClassName(className);
            newClass.setDescription(description);
            newClass.setMasterId(masterId);
            newClass.setIsDeleted(false);
            classMapper.insert(newClass);
            return "success";
        }catch (NullPointerException ne){
            return "needLogin";
        }catch (Exception e){
            e.printStackTrace();
        }
        return "error";
    }
    /**
     * @author yuruiqiao
     * @date 2020/2/12
     * @description 用于生成邀请码
     */
    @RequestMapping(path = "/createCode")
    public String createCode(@RequestParam("num") int num,HttpSession session,@RequestParam("classId")int classId){
        try{
            int creatorId = ((User) session.getAttribute("user")).getUserId();
            List<InviteCode> codes = inviteCodeMapper.inviteCodeQueryByClassId(classId);
            if(codes.size()+num<=50){
                for (int i = 0; i <num; i++) {
                    InviteCode inviteCode = new InviteCode();
                    inviteCode.setClassId(classId);
                    inviteCode.setCreatorId(creatorId);
                    inviteCode.setCode(UUID.randomUUID().toString());
                    inviteCode.setIsUsed(false);
                    inviteCodeMapper.insert(inviteCode);
                }
                return "success";
            }else if(codes.size()<50){//如果加上num个数超出了,但是座位暂时没有满
                for (int i = 0; i <50-codes.size(); i++) {
                    InviteCode inviteCode = new InviteCode();
                    inviteCode.setClassId(classId);
                    inviteCode.setCreatorId(creatorId);
                    inviteCode.setCode(UUID.randomUUID().toString());
                    inviteCode.setIsUsed(false);
                    inviteCodeMapper.insert(inviteCode);
                }
                return "success";
            }else{//表示座位已经满了
                return "full";
            }
        }catch (NullPointerException ne){
            return "needLogin";
        }catch (Exception e){
            e.printStackTrace();
        }
        return "false";
    }
    /**
     * @author yuruiqiao
     * @date 2020/2/13
     * @description 用于填写邀请码
     */
    @RequestMapping(path = "/fillCode")
    public String fillCode(HttpSession session,int classId,String code){
        try{
            int receiverId = ((User) session.getAttribute("user")).getUserId();
            List<InviteCode> inviteCodes = inviteCodeMapper.inviteCodeQueryByClassId(classId);
            for (InviteCode inviteCode : inviteCodes) {
                if (inviteCode.getCode().equals(code)) {
                    if (!inviteCode.getIsUsed()) {
                        inviteCode.setReceiverId(receiverId);
                        inviteCode.setIsUsed(true);
                        inviteCodeMapper.updateByPrimaryKey(inviteCode);
                        //下面将该同学放入该班级
                        JoinClass joinClass = new JoinClass();
                        joinClass.setClassId(classId);
                        joinClass.setUserId(receiverId);
                        joinClass.setIsTop(false);
                        joinClassMapper.insert(joinClass);
                        return "success";
                    }else{
                        return "used";//表示已经使用过了
                    }
                }
            }
            return "wrong";//表示填写错误
        }catch (NullPointerException ne){
            return "needLogin";
        }catch (Exception e){
            e.printStackTrace();
        }
        return "false";
    }
}
